import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Created by Administrator on 2018/6/16.
 */
public class RegisterServlet extends HttpServlet {


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //得到用户的输入的信息
        String id = request.getParameter("id");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String phone = request.getParameter("phone");

        // 3+1
        Connection connection = null;
        Statement statement = null;

        String sql = "insert into t_users(id,username, password,TEL) values('"+id+"','"+username+"','"+password+"','"+phone+"')";

        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/part_time_job?serverTimezone=UTC",
                    "root",
                    "123456"
            );

            statement = connection.createStatement();

            statement.execute(sql);


            statement.close();
            connection.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        response.setCharacterEncoding("gbk");

        PrintWriter out = response.getWriter();
        out.write("<h1>成功注册用户！</h1>");
        out.write("<a href='homePage.jsp'>返回首页</a>");

    }
}